package com.google.project.client.Director;

import java.util.List;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.project.shared.TaskRequireDTO;

public class DRTHumanResourceReq extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField(provided=true) CellTable<TaskRequireDTO> ctbHumanResourceReq = new CellTable<TaskRequireDTO>();
	@UiField SimplePager pager;
//	private List<HRReqProjectDTO> hrReqList = new ArrayList<HRReqProjectDTO>();

	interface Binder extends UiBinder<Widget, DRTHumanResourceReq> {
	}

	public DRTHumanResourceReq() {
		initWidget(binder.createAndBindUi(this));
		bindData();
	}
	
	public void bindData() {
//		taskRequireService.getListTaskReq( new AsyncCallback<List<TaskRequireDTO>>() {
//			
//			@Override
//			public void onSuccess(List<TaskRequireDTO> result) {
//				taskRequireDTOList = result;
//				loadData(result);
//				initTable();
//			}
//			
//			@Override
//			public void onFailure(Throwable caught) {
//			}
//		}); 
	}
	
	public void loadData(List<TaskRequireDTO> taskRequireDTO) {
		// Create a data provider.
		ListDataProvider<TaskRequireDTO> dataProvider = new ListDataProvider<TaskRequireDTO>();
 
		List<TaskRequireDTO> list = dataProvider.getList();
		for(int i = 0; i < taskRequireDTO.size(); i++) {
			taskRequireDTO.get(i).setIndex(i+1);
			list.add(taskRequireDTO.get(i));
		}
		// SimplePager
		pager.setDisplay(ctbHumanResourceReq);
		// Connect the table to the data provider.
		dataProvider.addDataDisplay(ctbHumanResourceReq);
	}
	
	//Init Cell Table
	public void initTable() {
		//STT
		TextColumn<TaskRequireDTO> indexColumn = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO object) {
				return object.getIndex()+"";
			}
		};
		
//		// Ma du an projectCodeColumn
//		Column<TaskRequireDTO, String> projectCodeColumn = new Column<TaskRequireDTO, String>(new ClickableTextCell()) {
//			@Override
//			public String getValue(TaskRequireDTO object) {
//				return object.getProjectCode();
//			}
//		};
//		projectCodeColumn.setFieldUpdater(new FieldUpdater<TaskRequireDTO, String>() {
//			@Override
//			public void update(int index, TaskRequireDTO project, String value) {
////				HMEntrypoint.get().HMProjectDetail(project.getProjectID());
//			}
//		});
//		projectCodeColumn.setCellStyleNames("celltable");
//		
		//Ten du an
		TextColumn<TaskRequireDTO> projectNameColumn = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO object) {
				return object.getProjectName();
			}
		};

		//So luong
		TextColumn<TaskRequireDTO> numberOfEmpColumn = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO object) {
				return object.getNumOfEmp()+"";
			}
		};

		ctbHumanResourceReq.setWidth("100%", true);
		ctbHumanResourceReq.setPageSize(5);
		ctbHumanResourceReq.setPageStart(0);
		ctbHumanResourceReq.setStyleName("ttable");
		
		ctbHumanResourceReq.addColumn(indexColumn, "STT");
		ctbHumanResourceReq.setColumnWidth(indexColumn, 45.0, Unit.PX);
		
//		ctbHumanResourceReq.addColumn(projectCodeColumn, "Mã dự án");
//		ctbHumanResourceReq.setColumnWidth(projectCodeColumn, 15.0, Unit.PCT);
		
		
		ctbHumanResourceReq.addColumn(projectNameColumn, "Tên dự án");
		ctbHumanResourceReq.setColumnWidth(projectNameColumn, 25.0, Unit.PCT);
		projectNameColumn.setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
		
		ctbHumanResourceReq.addColumn(numberOfEmpColumn, "Số lượng");
		ctbHumanResourceReq.setColumnWidth(numberOfEmpColumn, 25.0, Unit.PCT);
		numberOfEmpColumn.setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
	}
}
